package com.gzhryc.shared_device.oem.h5.mng.controllers.market.account;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccount;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountService;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccountWithdrawOrder;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountWithdrawOrderService;
import com.gzhryc.net_marketing.cloud.account.services.dto.CloudPayAccountWithdrawOrderSearch;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class CloudPayAccountWithdrawOrderController {

    @Route(value = { "/account/withdrawOrder/list.html" })
    public String list(HttpServletRequest request) {
        return "/account/withdrawOrder/list.html";
    }

    @Route(value = { "/account/withdrawOrder/list.data" })
    public ListResult<CloudPayAccountWithdrawOrder> listData(HttpServletRequest request) {
        ListResult<CloudPayAccountWithdrawOrder> result = new ListResult<CloudPayAccountWithdrawOrder>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountWithdrawOrderSearch search = WebUtils.getParameter(request, CloudPayAccountWithdrawOrderSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudPayAccountWithdrawOrderService.self().countBySearch(search));
        result.getBody().setData(CloudPayAccountWithdrawOrderService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/account/withdrawOrder/info.html" })
    public String info(HttpServletRequest request) {
        String tradeNo = WebUtils.getString(request,"tradeNo");
        if(StringTools.isNotBlank(tradeNo)){
            CloudPayAccountWithdrawOrder withdrawOrder = CloudPayAccountWithdrawOrderService.self().getByTradeNo(tradeNo);
            if(withdrawOrder != null){
                WebUtils.setAttribute(request,withdrawOrder);

                CloudPayAccount payAccount = CloudPayAccountService.self().getById(withdrawOrder.getAccountId());
                if(payAccount != null){
                    request.setAttribute("account",payAccount);
                }
            }
        }
        return "/account/withdrawOrder/info.html";
    }

    /**
     * 驳回
     * @param request
     * @return
     */
    @Route(value = { "/account/withdrawOrder/reject.do" })
    public AjaxResult doReject(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"处理失败，请稍后再试");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        String tradeNo = WebUtils.getString(request,"tradeNo");
        String rejectReason = WebUtils.getString(request,"rejectReason");
        if(StringTools.isNotBlank(tradeNo) && StringTools.isNotBlank(rejectReason)) {
            try {
                if (CloudPayAccountWithdrawOrderService.self().reject(tradeNo,rejectReason,manager)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/account/withdrawOrder/success.do" })
    public AjaxResult doSuccess(HttpServletRequest request, HttpSession session) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"处理失败，请稍后再试");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        String tradeNo = WebUtils.getString(request,"tradeNo");
        Integer handleType = WebUtils.getInteger(request,"handleType");
        if(StringTools.isNotBlank(tradeNo) && handleType != null) {
            if(handleType == 2){
                Integer transferType = WebUtils.getInteger(request,"tradeNo");
                String transferAccount = WebUtils.getString(request,"transferAccount");
                String transferTradeNo = WebUtils.getString(request,"transferTradeNo");
                if(transferType != null && StringTools.isNotBlank(transferAccount) && StringTools.isNotBlank(transferTradeNo)) {
                    try {
                        if (CloudPayAccountWithdrawOrderService.self().manualProcessing(tradeNo, transferType
                                , transferAccount, transferTradeNo, manager)) {
                            result.setCode(DtoConstants.SUCCESS, null);
                        }
                    } catch (LogicException e) {
                        result.setMsg(e.getMessage());
                    }
                }else{
                    result.setMsg("请填写人工转账的相关信息");
                    return result;
                }
            }else {
                //自动处理
                try {
                    if (CloudPayAccountWithdrawOrderService.self().autoProcessing(tradeNo,manager)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setMsg(e.getMessage());
                }
            }
        }
        return result;
    }
}
